package top.tangyh.lamp.care.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.tangyh.lamp.care.mapper.BPgRecordDetailMapper;
import top.tangyh.lamp.care.service.BPgRecordDetailService;
import top.tangyh.basic.base.service.impl.SuperServiceImpl;
import top.tangyh.lamp.care.manager.BPgRecordDetailManager;
import top.tangyh.lamp.care.entity.BPgRecordDetail;
import top.tangyh.lamp.care.service.BPgSrxService;
import top.tangyh.lamp.care.vo.result.BPgRecordResultVO;
import top.tangyh.lamp.care.vo.save.BPgRecordDetailSaveVO;
import top.tangyh.lamp.care.vo.save.PgRecordDetailVo;
import top.tangyh.lamp.care.vo.save.SrxVO;
import top.tangyh.lamp.care.vo.update.BPgRecordDetailUpdateVO;
import top.tangyh.lamp.care.vo.result.BPgRecordDetailResultVO;
import top.tangyh.lamp.care.vo.query.BPgRecordDetailPageQuery;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务实现类
 * 评估评分明细
 * </p>
 *
 * @author zqb
 * @date 2024-07-08 16:40:03
 * @create [2024-07-08 16:40:03] [zqb] [代码生成器生成]
 */
@Slf4j
@RequiredArgsConstructor
@Service
@Transactional(readOnly = true)
public class BPgRecordDetailServiceImpl extends SuperServiceImpl<BPgRecordDetailManager, Long, BPgRecordDetail> implements BPgRecordDetailService {

    private  final BPgRecordDetailMapper bPgRecordDetailMapper;

    private final BPgSrxService bPgSrxService;
    @Override
    public List<PgRecordDetailVo> selectByRecordId(Long recordId) {
        LambdaQueryWrapper<BPgRecordDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BPgRecordDetail::getPgRecordId,recordId);
        List<BPgRecordDetail> recordDetails = bPgRecordDetailMapper.selectList(queryWrapper);
        List<PgRecordDetailVo> detailVos = BeanUtil.copyToList(recordDetails, PgRecordDetailVo.class); ;

        detailVos.forEach(detailVo ->{
            SrxVO srxVO = bPgSrxService.selectBySrxId(detailVo.getPgSrxId());
            if(srxVO != null) {
                detailVo.setSrxVO(srxVO);
            }
        });
        return detailVos;
    }

    @Override
    public List<PgRecordDetailVo> selectDetailByRecordIds(Set<Long> sids) {
        LambdaQueryWrapper<BPgRecordDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(BPgRecordDetail::getPgRecordId,sids);
        List<BPgRecordDetail> recordDetails = bPgRecordDetailMapper.selectList(queryWrapper);
        if(CollectionUtil.isNotEmpty(recordDetails)){
            List<PgRecordDetailVo> detailVos =BeanUtil.copyToList(recordDetails, PgRecordDetailVo.class); ;

            Set<Long> pgsrxids = recordDetails.stream().map(BPgRecordDetail::getPgSrxId).collect(Collectors.toSet());
            List<SrxVO> svos = bPgSrxService.selectBySrxIds(pgsrxids);
            Map<Long,List<SrxVO>> datamap = null;
            if(CollectionUtil.isNotEmpty(svos)){
                datamap = svos.stream().collect(Collectors.groupingBy(SrxVO::getId));
            }
            if(null == datamap){
                datamap = new HashMap<>();
            }
            for(PgRecordDetailVo dvo : detailVos){
                List<SrxVO> srx = datamap.get(dvo.getPgSrxId());
                if(CollectionUtil.isNotEmpty(srx)){
                    dvo.setSrxVO(srx.get(0));
                }
            }
            return detailVos;
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long recordId) {
        LambdaQueryWrapper<BPgRecordDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BPgRecordDetail::getPgRecordId,recordId);
        List<BPgRecordDetail> pgRecordDetails = bPgRecordDetailMapper.selectList(wrapper);
        if(CollectionUtil.isNotEmpty(pgRecordDetails)){
            bPgRecordDetailMapper.delete(wrapper);
        }
    }
}


